package com.wwwarehouse.contract.program_operation.resource_factor_ship;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.MotionEvent;
import android.view.View;

import com.wwwarehouse.contract.bean.CustomerAndResBean;
import com.wwwarehouse.contract.bean.CustomerAndResListBean;
import com.wwwarehouse.contract.core.BaseViewPagerFragment;
import com.wwwarehouse.contract.core.ContractConstant;
import com.wwwarehouse.contract.event.CheckResourceFactorShipEvent;
import com.wwwarehouse.contract.event.ResourceFactorShipCheckEvent;

import java.util.List;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;

/**
 * Created by feng.gao on 2017/9/21.
 * 资源要素关系页
 */

public class ResourceFactorShipViewPagerFragment extends BaseViewPagerFragment implements View.OnClickListener {
	private CustomerAndResBean mCustomerAndResBean;

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		EventBus.getDefault().register(this);
		mCustomScroll.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				return true;
			}
		});
		mConfirmBtn.setVisibility(View.VISIBLE);
		mConfirmBtn.setOnClickListener(this);

	}

	@Override
	public void onResponse(String response, int what) {

	}



	@Override
	public void setTitle() {
		if (peekFragment() instanceof ResourceFactorShipViewPagerFragment) {
			mActivity.setTitle(R.string.associate_resource_factor_with_supply);
		}
	}

	@Override
	public void requestDatas() {
		if (getArguments() != null && getArguments().getSerializable(ContractConstant.KEY_BUNDLE_VIEW_PAGER_DATA) != null) {
			mCustomerAndResBean = (CustomerAndResBean) getArguments().getSerializable(ContractConstant.KEY_BUNDLE_VIEW_PAGER_DATA);
			if (mCustomerAndResBean != null && mCustomerAndResBean.getListBeen() != null) {

				//上次关联过的数据
				if (getArguments() != null && getArguments().getSerializable(ContractConstant.KEY_BUNDLE_LAST_RESCOURCE_FACTOR_SHIP_CHECKED) != null) {
					CustomerAndResBean customerAndResBean = (CustomerAndResBean) getArguments().getSerializable(ContractConstant.KEY_BUNDLE_LAST_RESCOURCE_FACTOR_SHIP_CHECKED);
					for (int i = 0; i < mCustomerAndResBean.getListBeen().size(); i++) {
						//遍历NAME
						for (int j = 0; j < customerAndResBean.getListBeen().size(); j++) {
							if (customerAndResBean.getListBeen().get(j).getName().equals(mCustomerAndResBean.getListBeen().get(i).getName())) {
								//大的部分在新数据里 接着判断小的部分数据
								for (int k = 0; k < mCustomerAndResBean.getListBeen().get(i).getList().size(); k++) {
									for (int l = 0; l < customerAndResBean.getListBeen().get(j).getList().size(); l++) {
										if (customerAndResBean.getListBeen().get(j).getList().get(l).getName().equals(mCustomerAndResBean.getListBeen().get(i).getList().get(k).getName())) {
											mCustomerAndResBean.getListBeen().get(i).getList().get(k).setSelected(customerAndResBean.getListBeen().get(j).getList().get(l).isSelected());
										}
									}
								}
							}
						}
					}
					for (int i = 0; i < mCustomerAndResBean.getListBeen().size(); i++) {
						for (int j = 0; j < mCustomerAndResBean.getListBeen().get(i).getList().size(); j++) {
							if (mCustomerAndResBean.getListBeen().get(i).getList().get(j).isSelected()) {
								canClick = true;
							}
						}
					}
					if (canClick) {
						mConfirmBtn.setEnabled(true);
						mConfirmBtn.setBackground(mActivity.getResources().getDrawable(R.drawable.blue_btn_circle));
					} else {
						mConfirmBtn.setEnabled(false);
						mConfirmBtn.setBackground(mActivity.getResources().getDrawable(R.drawable.btn_cir));
					}

				}
				setData(mCustomerAndResBean, mCustomerAndResBean.getListBeen().size(), 5 , new ResourceFactorShipFragment().getClass().getName() , false);
			}

		}


	}

	boolean canClick;
	public void onEventMainThread(ResourceFactorShipCheckEvent resourceShipCheckEvent){
		int position = resourceShipCheckEvent.getPosition();
		List<CustomerAndResListBean.Customers> resourceShipList = resourceShipCheckEvent.getResourceShipList();
		List<CustomerAndResListBean.Customers> list = mCustomerAndResBean.getListBeen().get(position).getList();
		list = resourceShipList;
		for (int i = 0; i < mCustomerAndResBean.getListBeen().size(); i++) {
			for (int j = 0; j < mCustomerAndResBean.getListBeen().get(i).getList().size(); j++) {
				if (mCustomerAndResBean.getListBeen().get(i).getList().get(j).isSelected()) {
					canClick = true;
				}
			}
		}
		if (canClick) {
			mConfirmBtn.setEnabled(true);
			mConfirmBtn.setBackground(mActivity.getResources().getDrawable(R.drawable.blue_btn_circle));
		} else {
			mConfirmBtn.setEnabled(false);
			mConfirmBtn.setBackground(mActivity.getResources().getDrawable(R.drawable.btn_cir));
		}
	}


	@Override
	public void onDestroy() {
		super.onDestroy();
		EventBus.getDefault().unregister(this);
	}


	@Override
	public void onClick(View v) {
		if (v == mConfirmBtn) {
			EventBus.getDefault().post(new CheckResourceFactorShipEvent(mCustomerAndResBean));
			popFragment();
		}
	}


}
